use reqwest::header::USER_AGENT;

const URL: &str = "https://dict.youdao.com/result";

pub async fn get_word(word: &str) -> Result<String, String> {
    let client = reqwest::Client::new();
    let response = client.get(URL)
        .query(&[("word", word), ("lang", "en")])
        .header(USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0")
        .send().await.map_err(|err| { err.to_string() })?;
    response.text().await.map_err(|_| { "Get response text failed.".to_string() })
}


#[cfg(test)]
mod test {
    use std::fs;
    use crate::request::get_word;

    #[tokio::test]
    async fn check_res() {
        fs::write("./test.html", get_word("Windows").await.unwrap()).expect("TODO: panic message");
    }
}